#SDK模型下载
# from modelscope import snapshot_download
# import os
# md_p='./bark-small'
# if not os.path.exists(md_p):
#     os.mkdir(md_p)
# model_dir = snapshot_download('angelala00/bark-small',cache_dir=md_p)

#下载向量化模型
# import os
# import torch
# from speechbrain.pretrained import EncoderClassifier
#
# # 设置 Hugging Face 镜像（关键步骤）
# os.environ["HF_ENDPOINT"] = "https://hf-mirror.com"
#
# spk_model_name = "speechbrain/spkrec-xvect-voxceleb"
#
# device = "cuda" if torch.cuda.is_available() else "cpu"  # 更安全的设备判断
# save_dir = os.path.join("./tmp", spk_model_name)
#
# print(f"模型将从镜像站下载并保存在：{save_dir}")
#
# speaker_model = EncoderClassifier.from_hparams(
#     source=spk_model_name,
#     run_opts={"device": device},
#     savedir=save_dir,
# )
#
# def create_speaker_embedding(waveform):
#     with torch.no_grad():
#         # 确保 waveform 是 tensor，并增加 batch 维度
#         if not isinstance(waveform, torch.Tensor):
#             waveform = torch.tensor(waveform)
#         if waveform.dim() == 1:
#             waveform = waveform.unsqueeze(0)  # [T] -> [1, T]
#         if waveform.dim() == 2 and waveform.shape[0] != 1:
#             waveform = waveform.T  # 确保 batch 在第一维
#
#         speaker_embeddings = speaker_model.encode_batch(waveform.to(device))
#         speaker_embeddings = torch.nn.functional.normalize(speaker_embeddings, dim=2)
#         speaker_embeddings = speaker_embeddings.squeeze().cpu().numpy()
#     return speaker_embeddings
#
# print("✅ 模型加载成功！现在可以使用 create_speaker_embedding() 函数。")

import os
from transformers import SpeechT5HifiGan

# -------------------------------
# 1. 设置国内镜像（可选，推荐国内用户使用）
import os
from transformers import SpeechT5HifiGan

# -------------------------------
# 1. 设置国内镜像（可选，推荐国内用户使用）
# -------------------------------
os.environ["HF_ENDPOINT"] = "https://hf-mirror.com"  # ✅ 修正：去掉末尾空格

# -------------------------------
# 2. 定义本地保存路径
# -------------------------------
local_vocoder_path = "./tmp/speecht5_hifigan"
os.makedirs(local_vocoder_path, exist_ok=True)

# -------------------------------
# 3. 从 Hugging Face 下载并保存到本地
# -------------------------------
print(f"正在从 microsoft/speecht5_hifigan 下载声码器...")
vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan")
vocoder.save_pretrained(local_vocoder_path)
print(f"✅ 声码器已成功下载并保存到: {local_vocoder_path}")